package com.candy.test;

import com.candy.dao.BlogMapper;
import com.candy.dao.StudentInfoMapper;
import com.candy.pojo.Blog;
import com.candy.pojo.StudentInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class DBDriver {
    @Test
    public void test12() {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
//            读取配置文件
            inputStream = Resources.getResourceAsStream(resource);
//           使用SqlSessionFactoryBuilder（工厂建造者） 根据配置信息（图纸）创建（build）工厂sqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//            由工厂SqlSessionFactory来创建session
            SqlSession session = sqlSessionFactory.openSession();
            StudentInfoMapper mapper = session.getMapper(StudentInfoMapper.class);
            StudentInfo stu=new StudentInfo();
            stu.setStuname("张");
            List<StudentInfo> ls=mapper.selectAllInfoByLike(stu);
            for (StudentInfo st : ls
            ) {
                System.out.println(st);
            }
            session.commit();
            System.out.println("第二次查询");
            List<StudentInfo> lss=mapper.selectAllInfoByLike(stu);
            for (StudentInfo st : lss
            ) {
                System.out.println(st);
            }
            session.commit();
            System.out.println("二级缓存测试-------------------");
            StudentInfoMapper stuMp=session.getMapper(StudentInfoMapper.class);

            stu.setStuname("张");
            List<StudentInfo> lst=stuMp.selectAllInfoByLike(stu);
            for (StudentInfo st : lst
            ) {
                System.out.println(st);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test11() {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
//            读取配置文件
            inputStream = Resources.getResourceAsStream(resource);
//           使用SqlSessionFactoryBuilder（工厂建造者） 根据配置信息（图纸）创建（build）工厂sqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//            由工厂SqlSessionFactory来创建session
            SqlSession session = sqlSessionFactory.openSession();
            BlogMapper mapper = session.getMapper(BlogMapper.class);

            List<Blog> ls=mapper.selectBlogDetails(1);
            for (Blog o : ls
            ) {
                System.out.println(o);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    @Test
    public void test10() {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
//            读取配置文件
            inputStream = Resources.getResourceAsStream(resource);
//           使用SqlSessionFactoryBuilder（工厂建造者） 根据配置信息（图纸）创建（build）工厂sqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//            由工厂SqlSessionFactory来创建session
            SqlSession session = sqlSessionFactory.openSession();
            StudentInfoMapper mapper = session.getMapper(StudentInfoMapper.class);
            StudentInfo studentInfo=new StudentInfo();
            studentInfo.setStuname("张");
            studentInfo.setStuaddress("青");
            List<StudentInfo> ls=mapper.selectAllInfoByParams2(studentInfo,true);
            for (StudentInfo o : ls
            ) {
                System.out.println(o);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test09(){
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
//            读取配置文件
            inputStream = Resources.getResourceAsStream(resource);
//           使用SqlSessionFactoryBuilder（工厂建造者） 根据配置信息（图纸）创建（build）工厂sqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//            由工厂SqlSessionFactory来创建session
            SqlSession session = sqlSessionFactory.openSession();
            StudentInfoMapper mapper=session.getMapper(StudentInfoMapper.class);
            List<StudentInfo> ls=   mapper.selectAllInfoByParams("张","青");
            for (StudentInfo o:ls
            ) {
                System.out.println(o);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test08(){
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
//            读取配置文件
            inputStream = Resources.getResourceAsStream(resource);
//           使用SqlSessionFactoryBuilder（工厂建造者） 根据配置信息（图纸）创建（build）工厂sqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//            由工厂SqlSessionFactory来创建session
            SqlSession session = sqlSessionFactory.openSession();
            StudentInfoMapper mapper=session.getMapper(StudentInfoMapper.class);
            StudentInfo studentInfo=new StudentInfo();
            studentInfo.setStuaddress("天");
            List<StudentInfo> ls=  mapper.selectAllInfoByLike2(studentInfo);
            for (StudentInfo o:ls
            ) {
                System.out.println(o);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test07(){
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
//            读取配置文件
            inputStream = Resources.getResourceAsStream(resource);
//           使用SqlSessionFactoryBuilder（工厂建造者） 根据配置信息（图纸）创建（build）工厂sqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//            由工厂SqlSessionFactory来创建session
            SqlSession session = sqlSessionFactory.openSession();
            StudentInfoMapper mapper=session.getMapper(StudentInfoMapper.class);
            StudentInfo studentInfo=new StudentInfo();
            studentInfo.setStuname("张");
//            studentInfo.setStuaddress("青");
          List<StudentInfo> ls=  mapper.selectAllInfoByLike(studentInfo);
            for (StudentInfo o:ls
                 ) {
                System.out.println(o);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test06(){
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
//            读取配置文件
            inputStream = Resources.getResourceAsStream(resource);
//           使用SqlSessionFactoryBuilder（工厂建造者） 根据配置信息（图纸）创建（build）工厂sqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//            由工厂SqlSessionFactory来创建session
            SqlSession session = sqlSessionFactory.openSession();
            StudentInfoMapper mapper=session.getMapper(StudentInfoMapper.class);
            List idList=new ArrayList();
            idList.add(1);
            idList.add(4);
            idList.add(9);
            idList.add(11);

            List<StudentInfo> ls=mapper.selectAllInfoByIds(idList);
            for (StudentInfo st:ls
                 ) {
                System.out.println(st);
            }



        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test05(){
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
//            读取配置文件
            inputStream = Resources.getResourceAsStream(resource);
//           使用SqlSessionFactoryBuilder（工厂建造者） 根据配置信息（图纸）创建（build）工厂sqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//            由工厂SqlSessionFactory来创建session
            SqlSession session = sqlSessionFactory.openSession();
            StudentInfoMapper mapper=session.getMapper(StudentInfoMapper.class);
            StudentInfo stu=new StudentInfo();
            stu.setStuname("郭芙蓉");
//            stu.setStupwd("955876532");
            stu.setStuaddress("上海");
//            stu.setStutel("16987456698");
//            执行新增操作
           int i= mapper.insertSelective(stu);
            System.out.println(i);
            session.commit();




        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test04(){
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
//            读取配置文件
            inputStream = Resources.getResourceAsStream(resource);
//           使用SqlSessionFactoryBuilder（工厂建造者） 根据配置信息（图纸）创建（build）工厂sqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//            由工厂SqlSessionFactory来创建session
            SqlSession session = sqlSessionFactory.openSession();
            StudentInfoMapper mapper=session.getMapper(StudentInfoMapper.class);
            StudentInfo stu=new StudentInfo();
            stu.setId(11);
//            stu.setStuname("佟湘玉");
//            stu.setStupwd("955876532");
            stu.setStuaddress("南京");
//            stu.setStutel("16987456698");
//            执行修改操作
            List<StudentInfo> ls= mapper.selectAllInfoByWhere(stu);
            for (StudentInfo s:ls
            ) {
                System.out.println(s);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test03(){
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
//            读取配置文件
            inputStream = Resources.getResourceAsStream(resource);
//           使用SqlSessionFactoryBuilder（工厂建造者） 根据配置信息（图纸）创建（build）工厂sqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//            由工厂SqlSessionFactory来创建session
            SqlSession session = sqlSessionFactory.openSession();
            StudentInfoMapper mapper=session.getMapper(StudentInfoMapper.class);
            StudentInfo stu=new StudentInfo();
            stu.setId(11);
//            stu.setStuname("佟湘玉");
//            stu.setStupwd("955876532");
//            stu.setStuaddress("南京");
//            stu.setStutel("16987456698");
//            执行修改操作
            List<StudentInfo> ls= mapper.selectAllInfoByParam(stu);
            for (StudentInfo s:ls
                 ) {
                System.out.println(s);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test02(){
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
//            读取配置文件
            inputStream = Resources.getResourceAsStream(resource);
//           使用SqlSessionFactoryBuilder（工厂建造者） 根据配置信息（图纸）创建（build）工厂sqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//            由工厂SqlSessionFactory来创建session
            SqlSession session = sqlSessionFactory.openSession();
            StudentInfoMapper mapper=session.getMapper(StudentInfoMapper.class);
            StudentInfo stu=new StudentInfo();
            stu.setId(11);
            stu.setStuname("佟湘玉");
            stu.setStupwd("6666666");
//            stu.setStuaddress("南京");
//            stu.setStutel("16987456698");
//            执行修改操作
            int i= mapper.updateByPrimaryKeySelective(stu);
            session.commit();
            System.out.println(i);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test01(){
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
//            读取配置文件
            inputStream = Resources.getResourceAsStream(resource);
//           使用SqlSessionFactoryBuilder（工厂建造者） 根据配置信息（图纸）创建（build）工厂sqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//            由工厂SqlSessionFactory来创建session
            SqlSession session = sqlSessionFactory.openSession();
            StudentInfoMapper mapper=session.getMapper(StudentInfoMapper.class);
//            通过主键id进行查询操作
            StudentInfo stu=mapper.selectByPrimaryKey(1);
            System.out.println(stu);
            System.out.println("=====================");
            SqlSession session2 = sqlSessionFactory.openSession();
            StudentInfoMapper mapper2=session2.getMapper(StudentInfoMapper.class);
//            通过主键id进行查询操作
            StudentInfo stu2=mapper2.selectByPrimaryKey(1);
            System.out.println(stu2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
